package com.gm.commonlib.base.baseui.baseListviewUI;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.TextView;

import com.gm.commonlib.R;
import com.gm.commonlib.views.toast.ToastUtil;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

public abstract class BaseGridFragment<
        DataType,
        NetworkResponse> extends BasePullToRefeshFragment<
        DataType,
        NetworkResponse> {

    public GridView mGridView;
    RefreshLayout mRefreshLayout;
    TextView tv_no_content;

    FrameLayout ll_latest_cargos;

    protected void notifyListDataSetChanged() {
        if (mBaseAdapter != null) {
            mBaseAdapter.notifyDataSetChanged();
        }
        if (tv_no_content!=null){
            if (mDatas.isEmpty()) {
                tv_no_content.setVisibility(View.VISIBLE);
            } else {
                tv_no_content.setVisibility(View.GONE);
            }
        }

    }

    public void setNumColumns(int num){
        if (mGridView!=null){
            mGridView.setNumColumns(num);
        }
    }

    public void setHorizontalSpacing(int Spacing){
        if (mGridView!=null){
            mGridView.setHorizontalSpacing(Spacing);
        }
    }

    public void setVerticalSpacing(int Spacing){
        if (mGridView!=null){
            mGridView.setVerticalSpacing(Spacing);
        }
    }

    public void initPullToRefreshListView() {
        mBaseAdapter = createTempBaseAdapter();
        mGridView.setAdapter(mBaseAdapter);
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));
        setPullToRefreshListViewListener();
    }

    public void setPullToRefreshListViewListener() {

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //下拉刷新
                onRefreshView();
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                loadMore();

            }
        });
    }


    public void loadMore() {
        if (mPageInfo != null) {
            if (mCurrentPage >= mPageInfo.getTotalPages()) {
                ToastUtil.showToast(getResources().getString(R.string.already_last_page));
                getDataOnCompleted();
            } else {
                requestPageData(mCurrentPage + 1, false);
            }
        } else {
            requestPageData(1, false);
        }
    }

    View bodyView;
    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        bodyView = LayoutInflater.from(getContext()).inflate(R.layout.base_grid_pulltorefresh, parent);
        mGridView = bodyView.findViewById(R.id.gridView);
        mRefreshLayout = bodyView.findViewById(R.id.refreshLayout);
        tv_no_content = bodyView.findViewById(R.id.tv_no_content);
        ll_latest_cargos=bodyView.findViewById(R.id.ll_latest_cargos);
        mIsCreateViewFinish=true;
        mGridView.setOnScrollListener(new AbsListView.OnScrollListener(){

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                onGridScrollStateChanged();
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        mGridView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                onTouchClick();
                return false;
            }
        });

    }

    public void onTouchClick(){

    }

    public void onGridScrollStateChanged(){

    }

    public void setBodyPadding(int left){
        ll_latest_cargos.setPadding(left,0,left,0);
    }

    public void setBodybg(int color){
        bodyView.setBackgroundColor(color);
    }


    protected void setRefreshAndLoadEnable(boolean isPull, boolean isLoadMore) {

        mRefreshLayout.setEnableRefresh(isPull);
        mRefreshLayout.setEnableLoadMore(isLoadMore);
        mRefreshLayout.setEnableAutoLoadMore(isLoadMore);

    }

    @Override
    public void getDataOnCompleted() {
        super.getDataOnCompleted();
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadMore();
    }

    public void setNoContentText(String text) {
        tv_no_content.setText(text);
    }


}
